<template>
	<view v-if="Object.keys(goods).length !== 0">
		<!-- 轮播图 -->
		<swiper circular :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
		  <swiper-item v-for="(item, i) in goods.pics" :key="i">
		    <image :src="item.pics_big" @click="preview(index)"></image>
		  </swiper-item>
		</swiper>
		<!-- 文字信息 -->
		<view class="info">
			<!-- 价格 -->
			<view class="price">￥{{goods.goods_price}}</view>
			<!-- 名称、收藏 -->
			<view class="mid">
				<view class="title">
					{{goods.goods_name}}
				</view>
				<view class="favi">
					<uni-icons color="gray" type="star" szie="20"></uni-icons>
					<text>收藏</text>
				</view>
			</view>
			<view class="yf">
				快递：免运费
			</view>
		</view>
		<!-- 商品详情 -->
		<!-- <div v-html="goods.goods_introduce"></div> -->
		<rich-text :nodes="goods.goods_introduce"></rich-text>
		<!-- 商品导航 -->
		<view class="goods-carts">
			<uni-goods-nav :fill="true"  :options="options" :buttonGroup="buttonGroup"  @click="onClick" @buttonClick="buttonClick" />
		</view>
	</view>
</template>

<script>
	import { mapMutations, mapGetters } from 'vuex'
	export default {
		data() {
			return {
				// 商品详情对象
				goods: {},
				options: [{
					icon: 'shop',
					text: '店铺'
				}, {
					icon: 'cart',
					text: '购物车',
					info: 2
				}],
					buttonGroup: [{
						text: '加入购物车',
						backgroundColor: '#ff0000',
						color: '#fff'
					},
					{
						text: '立即购买',
						backgroundColor: '#ffa200',
						color: '#fff'
				}]
			}
		},
		onLoad(options) {
			// 获取商品 Id
			// console.log('options', options)
			this.getGoods(options.goods_id)
		},
		computed: {
			...mapGetters('m_cart', ['allGoodsCount'])
		},
		methods: {
			...mapMutations('m_cart', ['updateAddCart']),
			// 左侧图片点击事件
			 onClick (e) {
				 // console.log(e)
				 if(e.content.text === '购物车') {
					 uni.switchTab({
					 	url: '/pages/cart/cart'
					 })
				 }
			},
			// 按钮的点击事件
			buttonClick (e) {
				// 点击加入购物车之后
				if(e.content.text === '加入购物车') {
					// 准备精简 商品数据
					const goods = {
						goods_id: this.goods.goods_id, // id
						goods_name: this.goods.goods_name, // 名称						
						goods_price: this.goods.goods_price, // 价格						
						goods_samall_log: this.goods.goods_small_logo, // 图片q					
						goods_count: 1,	// 数量						
						goods_status: true // 勾选状态
					}
				// 通过 this 调用映射过来的 updateAddCart 方法，把商品信息对象存储到购物车中
				this.updateAddCart(goods)
				}
			},
			// 预览轮播图
			preview(index) {
				uni.previewImage({
					current: index,
					urls: this.goods.pics.map(e => {
						return e.pics_big
					})
				})
			},
			// 获取商品详情
			async getGoods(goods_id) {
				const {data: {message,meta}} = await uni.$http.get('/api/public/v1/goods/detail?goods_id=' + goods_id)
				if (meta.status !== 200) return uni.$showMsg()
				// 去掉图片之间的间隙
				message.goods_introduce = message.goods_introduce.replace(/<img/g, '<img style="display:block"')
				this.goods = message
			}
		},
		watch: {
			allGoodsCount: {
				handler(newCount) {
					this.options[1].info = newCount
				},
				immediate: true
			}
		}
	}
</script>

<style lang="scss">
	.goods-carts {
			position: fixed;
			left: 0;
			right: 0;
			bottom: 0;
		}
	.info {
		padding: 6px;
		.price {
			color: #ea4350;
			font-size: 20px;
		}
		.mid {
			margin: 6px 0;
			font-size: 12px;
			display: flex;
			.favi {
				border-left: 1px solid #eee;
				width: 120px;
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				color: gray;
			}
		}
		.yf {
			font-size: 10px;
			color: gray;
		}
	}
	// 轮播图
swiper {
  height: 750rpx;

  image {
    width: 100%;
    height: 100%;
  }
}
</style>
